$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'StudentAttendenceDetails/Select',
        datatype: "json",
        colModel: [
            { label: '日期', name: 'time', index:'time',width: 40,key: true, sortable:false },
            { label: '学生姓名', name: 'studentName', index:'student_name',width: 40, sortable:false },
            { label: '学号', name: 'studentId', index:'student_id',width: 40, sortable:false },
            { label: '班级名称', name: 'className',index:'class_name', width: 40, sortable:false },
            { label: '专业', name: 'studentProfessionName', index:'student_department',width: 40 , sortable:false},
            { label: '院系', name: 'studentDepartment', index:'student_department',width: 40 , sortable:false},
             { label: '课程名称', name: 'courseName',index:'course_name', width: 40 , sortable:false},
            { label: '节次', name: 'courseWhichIndex', index:'which_index',width: 30 , sortable:false},
            { label: '授课老师', name: 'courseTeacherName',index:'course_name', width: 30 , sortable:false},
            { label: '考勤状态', name: 'attendenceState', index:'attendence_state',width: 30, sortable:false, formatter: function(value, options, row){
                var result;
                if( value == '出勤')
                    result= '<span class="table-btn1 table-success">出勤</span>';
               else if( value == '迟到')
                    result= '<span class="table-btn1 table-late">迟到</span>';
                else if( value == '早退')
                    result= '<span class="table-btn1 table-early">早退</span>';
                else if( value == '旷课')
                    result= '<span class="table-btn1 table-early">旷课</span>';
                else if( value == '请假')
                    result= '<a class="table-btn1 table-absenteeism" src="'+row.imagePath+'">请假</a>';
                else if( value == '病假')
                    result= '<a class="table-btn1 table-absenteeism" src="'+row.imagePath+'">病假</a>';
                else if( value == '公假')
                    result= '<a class="table-btn1 table-absenteeism" src="'+row.imagePath+'">公假</a>';
                else if( value == '私假')
                    result= '<a class="table-btn1 table-absenteeism" src="'+row.imagePath+'">私假</a>';
                if(row.isAttendance==2)
                {
                    result=result+'<span class="corner">2</span>';
                }
                return result;

            }},
             { label: '提交人', name: 'operatorName', index:'operator_name',width: 40, sortable:false },
            { label: '提交时间', name: 'updateTime', index:'update_time',width: 60, sortable:false },
         ],
        viewrecords: true,
        height: '100%',
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        // rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        }
        ,
        ondblClickRow:function(rowid){
            var rowData = $("#jqGrid").jqGrid('getRowData',rowid);
            var rowName =$("#jqGrid").jqGrid('getGridParam','colNames');
            var s="";
            var i=2;
            for(var key in rowData){
                s=s+rowName[i++]+':'+rowData[key]+'<br> ';
            }
           // alert(s);
        }
    });
    $("#exportExcel").attr('href',baseURL+'StudentAttendenceDetails/export?token='+token);
    $.ajax({
        url: baseURL + "/college/getAllStudentCollege2",
        data: {},
        success: function (r) {
            $("#College").append("<option value=''>请选择学院</option>");
            for (i = 0; i < r.list.length; i++) {
                var tname = r.list[i].collegeName;
                var tid = r.list[i].collegeName;
                $("#College").append("<option value='" + tid + "'>" + tname + "</option>");
            }
        }
    });
    $("#College").change(function () {
        $("#Class").empty();
        var college=$("#College").val();
        $.ajax({
            url: baseURL + "/college/getClassByCollege",
            data: {
                college:college
            },
            success: function (r) {
                $("#Class").append("<option value=''>请选择班级</option>");
                for (i = 0; i < r.list.length; i++) {
                    var tname = r.list[i];
                    var tid = r.list[i];
                    $("#Class").append("<option value='" + tid + "'>" + tname + "</option>");
                }
            }
        });
        $("#Profession").empty();
        var college=$("#College").val();
        $.ajax({
            url: baseURL + "/college/getProfessionByCollege",
            data: {
                college:college
            },
            success: function (r) {
                $("#Profession").append("<option value=''>请选择专业</option>");
                for (i = 0; i < r.list.length; i++) {
                    var tname = r.list[i];
                    var tid = r.list[i];
                    $("#Profession").append("<option value='" + tid + "'>" + tname + "</option>");
                }
            }
        });
    });
    $("#Profession").change(function () {
        $("#Class").empty();
        var college=$("#College").val();
        var profession=$("#Profession").val();
        $.ajax({
            url: baseURL + "/college/getClassByCollege",
            data: {
                college:college,
                profession:profession
            },
            success: function (r) {
                $("#Class").append("<option value=''>请选择班级</option>");
                for (i = 0; i < r.list.length; i++) {
                    var tname = r.list[i];
                    var tid = r.list[i];
                    $("#Class").append("<option value='" + tid + "'>" + tname + "</option>");
                }
            }
        });
    });
    $("#Class").change(function () {
        $("#Student").empty();
        var college=$("#College").val();
        var classname=$("#Class").val();
        $.ajax({
            url: baseURL + "/StudentInfo/getStudentInfoByClass",
            data: {
                classname:classname,
            },
            success: function (r) {
                $("#Student").append("<option value=''>请选择学生</option>");
                for (i = 0; i < r.list.length; i++) {
                    var tname = r.list[i].studentName;
                    var tid = r.list[i].studentSchoolId;
                    $("#Student").append("<option value='" + tid + "'>" + tname + "</option>");
                }
            }
        });
        $("#Course").empty();
        $.ajax({
            url: baseURL + "/course/getCourseByClass",
            data: {
                classname:classname,
            },
            success: function (r) {
                $("#Course").append("<option value=''>请选择课程</option>");
                for (i = 0; i < r.list.length; i++) {
                    var tname = r.list[i];
                    var tid = r.list[i];
                    $("#Course").append("<option value='" + tid + "'>" + tname + "</option>");
                }
            }
        });
    });
});
function setDate(id) {
    var Nowdate=new Date();
    M=Number(Nowdate.getMonth())+1;
    var today=Nowdate.getFullYear()+"-"+fix(M)+"-"+fix(Nowdate.getDate());
    if(id=='1'){
        $("#startDate").val(today);
        $("#endDate").val(today);
    }else if(id=='2'){
        $("#startDate").val(showWeekFirstDay());
        $("#endDate").val(today);
    }if(id=='3'){
        $("#startDate").val(showMonthFirstDay());
        $("#endDate").val(today);
    }
    vm.reload();
}
function showWeekFirstDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    M=Number(WeekFirstDay.getMonth())+1
    return WeekFirstDay.getFullYear()+"-"+fix(M)+"-"+fix(WeekFirstDay.getDate());
}
function showWeekLastDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
    M=Number(WeekLastDay.getMonth())+1
    return WeekLastDay.getFullYear()+"-"+M+"-"+WeekLastDay.getDate();
}
function showMonthFirstDay()
{
    var Nowdate=new Date();
    var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);
    M=Number(MonthFirstDay.getMonth())+1
    return MonthFirstDay.getFullYear()+"-"+fix(M)+"-"+fix(MonthFirstDay.getDate());
}
function fix(num){
    if(num<10)
        return "0"+num;
    else return ""+num;
}
function showMonthLastDay()
{
    var Nowdate=new Date();
    var MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1,1);
    var MonthLastDay=new Date(MonthNextFirstDay-86400000);
    M=Number(MonthLastDay.getMonth())+1
    return MonthLastDay.getFullYear()+"-"+M+"-"+MonthLastDay.getDate();
}
function goString(data){
        var temp=""
        for(let i in data){
            if(i==data.length-1){
                temp+=('\''+data[i]+'\'')
            }else{
                temp+=('\''+data[i]+'\'')+','
            }
        }
        return temp
    }
var vm = new Vue({
    el:'#rrapp',
    data:{
        collageData:null,
        yearData:null,
        termData:null,
        weekData:null,
        dayData:null,
        indexData:null,
        collageValue:[],
        yearValue:[],
        termValue:[],
        weekValue:[],
        dayValue:[],
        indexValue:[],
        collageOptions:[],
        yearOptions:[],
        termOptions:[],
        weekOptions:[],
        dayOptions:[],
        indexOptions:[],
        q:{
            key: null
        },
        showList: true,
        title: null,
        StudentAttendenceDetails: {},
        updateTime: null
    },
    mounted(){
             $.ajax({
                url: baseURL + "/college/getAllcollege",
                data: {},
                success: function (r) {
                    for (i = 0; i < r.list.length; i++) {
                        var tempJson={
                            'value':r.list[i].collegeName,
                            'label':r.list[i].collegeName
                        }
                        vm.collageOptions.push(tempJson)
                    }
                }
            });
            $.ajax({
                url: baseURL + "/term/getSchoolTerm",
                data: {},
                success: function (r) {
                    for (i = 0; i < r.list.length; i++) {
                         var tempJson={
                            'value':r.list[i].schoolAcademicYear,
                            'label':r.list[i].schoolAcademicYear
                        }
                        vm.yearOptions.push(tempJson)
                    }
                }
             });
             var listTerm=["1","2"];
             for (i = 0; i < listTerm.length; i++) {
                var tempJson={
                    'value':listTerm[i],
                    'label':"第"+listTerm[i]+"学期"
                }
                this.termOptions.push(tempJson)
             }
             for (i = 1; i < 21; i++) {
                var tempJson={
                    'value':i,
                    'label':"第"+i+"周"
                }
                this.weekOptions.push(tempJson)
             }
             for (i = 1; i < 8; i++) {
               var tempJson={
                    'value':i,
                    'label':"星期"+i
                }
                this.dayOptions.push(tempJson)
             }
             for (i = 1; i < 11; i+=2) {
                var tempJson={
                    'value':i,
                    'label':"第"+i+"节"
                }
                this.indexOptions.push(tempJson)
             }
        },
    methods: {
        query: function () {
            var startDate=$("#startDate").val();
            var endDate=$("#endDate").val();
            var college=$("#College").val();
            var profession=$("#Profession").val();
            var class1=$("#Class").val();
            var studentId=$("#Student").val();
            var course=$("#Course").val();
            var state=$("#State").val();
            var href=baseURL+'StudentAttendenceDetails/export?token='+token+
                "&startDate="+startDate+
                "&endDate="+ endDate+
                "&college="+college+
                "&profession="+profession+
                "&class="+class1+
                "&studentId="+studentId+
                "&course="+course+
                "&state="+state;
            $("#exportExcel").attr('href',href);
            vm.reload();
        },
        reload: function () {
            vm.collageData=goString(vm.collageValue);
            vm.yearData=goString(vm.yearValue);
            vm.termData=goString(vm.termValue);
            vm.weekData=goString(vm.weekValue);
            vm.dayData=goString(vm.dayValue);
            vm.indexData=goString(vm.indexValue);
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    "collegeName":vm.collageData,
                    "academicYear":vm.yearData,
                    "academicTerm":vm.termData,
                    "academicWeek":vm.weekData,
                    "academicDay":vm.dayData,
                    "academicIndex":vm.indexData,
                    'studentName': vm.q.key,
                    'startDate': $("#startDate").val(),
                    'endDate': $("#endDate").val(),
                    'college': $("#College").val(),
                    'profession':$("#Profession").val(),
                    'studentId':$("#Student").val(),
                    'class': $("#Class").val(),
                    'course':$("#Course").val(),
                    'state':$("#State").val()
                },
                page:page
            }).trigger("reloadGrid");
        },

        }
});